package com.briup.sms.service.impl;

import com.briup.sms.dao.AdminMapper;
import com.briup.sms.entity.Admin;
import com.briup.sms.service.AdminService;
import com.briup.sms.utils.JWTUtils;
import com.briup.sms.utils.ResultCode;
import com.briup.sms.utils.SMSException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminMapper mapper;

    @Override
    public String login(Admin admin) {
        //先查询用户是否存在
        Admin user = mapper.selectByUsername(admin.getUsername());
        if (user == null)
            throw new SMSException(ResultCode.USER_NOT_EXIST);
//        用户状态
        if (!user.getStats()) {
            throw new SMSException(ResultCode.USER_ACCOUNT_FORBIDDEN);
        }
//        用户名密码匹配的问题
        if (user.getPassword().equals(admin.getPassword())) {
            Map<String,Object> map = new HashMap();
            map.put("username",user.getUsername());
            return JWTUtils.generator(map);
        } else {
            throw new SMSException(ResultCode.USER_LOGIN_ERROR);
        }

    }
}
